/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AplManager.controller;
import AplManager.views.*;
import AplManager.models.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 *
 * @author user
 */
public class AddPlayerController {
     private AddPlayer view;
      private TableDisplay tableDisplayView;
     private Team model;
    private ActionListener actionListener;
    private DBcontroller dbcontrol;
    
    public AddPlayerController(Team model,AddPlayer  view,TableDisplay tableDisplayView,DBcontroller dbcontrol){
       this.model = model;
        this.view  = view;
         this.tableDisplayView  = tableDisplayView;
         this.dbcontrol = dbcontrol;      
    }
    
    public void control(){        
        actionListener = new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) { 
                  if(e.getSource().equals(view.getCancelButton())){
                  //AddPlayer addplayer = new AddPlayer();
                  //addplayer.setVisible(true);
                 view.setVisible(false);
                  }
                   if(e.getSource().equals(view.getSaveButton())){
                     String fname = view.getFirstName();
                     String lname = view.getLastName();
                     int num = view.getNumber();
                     String position = view.getPosition();
                     Player player = new Player(fname,lname,position,num);
                     model.addPlayer(player);
                     //tableDisplayView.resetTable();
                     //for(int i=0; i <model.getCurSize();i++){
                     Object[] row = {model.getPlayers()[model.getCurSize()-1].getFirstName(),model.getPlayers()[model.getCurSize()-1].getlastName(),
                                model.getPlayers()[model.getCurSize()-1].getNumber()+"",model.getPlayers()[model.getCurSize()-1].getPosition(),
                                 model.getPlayers()[model.getCurSize()-1].getGoals()+"", model.getPlayers()[model.getCurSize()-1].getAssists()+"",
                                 model.getPlayers()[model.getCurSize()-1].isInStartingLineUp()};
                     tableDisplayView.addRows(row);
          
                     
                    // }
                      view.setVisible(false);
                  }
              }
        };                
        view.getCancelButton().addActionListener(actionListener);
        view.getSaveButton().addActionListener(actionListener);
    }
}
